package com.wzy.orderSystem.service.impl;

import com.wzy.orderSystem.dao.FoodDao;
import com.wzy.orderSystem.pojo.order;
import com.wzy.orderSystem.service.opOrder;

import java.sql.Timestamp;
import java.util.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class opOrderImpl implements opOrder {
    @Override
    public List<order> selectById(int id) throws SQLException {
        ArrayList<order> orders = new ArrayList<>();
        ResultSet resultSet = null ;
        try {
            String sql = "SELECT * FROM `order` WHERE id = ?";
            Object[] obj = {id};
            resultSet = FoodDao.selectByArgs(sql, obj);
            while(resultSet.next()){
                int orderId = resultSet.getInt(1);
                int foodId = resultSet.getInt(2);
                String customName = resultSet.getString(3);
                String phone = resultSet.getString(4);
                String food = resultSet.getString(5);
                int foodNum = resultSet.getInt(6);
                String foodMark = resultSet.getString(7);
                Date date = resultSet.getDate(8);
                String address = resultSet.getString(9);
                String foodMoney = resultSet.getString(10);
                String foodState = resultSet.getString(11);

                order order = new order();
                order.setId(orderId);
                order.setFoodId(foodId);
                order.setCustomName(customName);
                order.setPhone(phone);
                order.setFood(food);
                order.setFoodNum(foodNum);
                order.setFoodMark(foodMark);
                order.setFood_date(date);
                order.setFoodAddress(address);
                order.setFoodMoney(foodMoney);
                order.setFoodState(foodState);

                orders.add(order);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            resultSet.close();
        }
        return orders;
    }

    @Override
    public int updateById(int id, String foodState) {
        int rows = 0;
        String sql = "UPDATE `order` SET food_state=? WHERE id=?";
        Object[] obj ={foodState,id};
        rows = FoodDao.updateByArgs(sql, obj);
        return rows;
    }

    @Override
    public int deleteById(int id) {
        int rows = 0;
        String sql = "DELETE FROM `order` WHERE id=?";
        Object[] obj ={id};
        rows = FoodDao.updateByArgs(sql, obj);
        return rows;
    }

    @Override
    public int insert(int id, int foodId, String customName, int phone, String food, int foodNum, String foodMark, Date foodDate,String address,String foodMoney,String foodState) {
        int rows = 0;
        String sql = "INSERT INTO `orderfood`.`order` (`id`, `food_id`, `custom_name`, `phone`, `food`, `food_num`, `food_mark`, `food_date`, `food_address`, `food_money`, `food_state`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        Object[] obj ={id,foodId,customName,phone,food,foodNum,foodMark,new Timestamp(foodDate.getTime()),address,foodMoney,foodState};
        rows = FoodDao.updateByArgs(sql, obj);
        return rows;
    }
}
